# -*- coding : "utf-8" -*-
# @姓名       : GUI
# @日期       : 2023/2/12 19:57
# @FileName  ： databases.py
# @Software  ： PyCharm
import logging
from commons import settings
import pymysql as MySQLdb

logger = logging.getLogger(__name__)


class DBServer:
    def __init__(self, *args, **kwargs):
        self.db = MySQLdb.connect(*args, **kwargs)
        self.c = self.db.cursor()  # 创建新的会话

    def execute_sql(self, sql):
        logging.info(f"{sql=}")
        self.c.execute(sql)  # 执行sql命令
        res = self.c.fetchone()  # 返回单行结果
        # res = self.c.fetchall()  # 返回多行结果
        return res


db = DBServer(
    host=settings.db_host,  # IP
    port=settings.db_port,  # 端口
    user=settings.db_user,  # 用户名
    password=settings.db_password,  # 密码
    database=settings.db_database,  # 库名
)
